﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MRSite.RedisCache
{
    public class ShanYuRedisManager
    {
        static ShanYuRedisManager()
        {
            InitializeManager();
        }

        private static void InitializeManager()
        {
            string[] WriteServerConStr = SplitString(RedisConfig.WriteServerConStr, ",");
            string[] ReadServerConStr = SplitString(RedisConfig.ReadServerConStr, ",");

            _pooledRedisClientManager = new PooledRedisClientManager(
                                            ReadServerConStr,
                                            WriteServerConStr,
                                            new RedisClientManagerConfig
                                            {
                                                MaxReadPoolSize = RedisConfig.MaxReadPoolSize,
                                                MaxWritePoolSize = RedisConfig.MaxWritePoolSize,
                                                AutoStart = RedisConfig.AutoStart
                                            });
        }
        private static string[] SplitString(string strSource, string split)
        {
            return strSource.Split(split.ToArray());
        }

        public static IRedisClient GetClient()
        {
            if (_pooledRedisClientManager == null)
            {
                InitializeManager();
            }
            return _pooledRedisClientManager.GetClient();
        }

        private static ShanYuRedisConfig RedisConfig = ShanYuRedisConfig.GetConfig();
        private static PooledRedisClientManager _pooledRedisClientManager;
    }
}
